package cn.fintecher.pangolin.service.manage.request;

import cn.fintecher.pangolin.common.enums.NoticeType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 查询公告请求参数
 * @Date: 10:07 2019/9/2
 */

@Data
public class FindNoticeRequest {
    @ApiModelProperty(value = "关键字")
    private String keyword;

    @ApiModelProperty(value = "发布人")
    private String name;

    @ApiModelProperty(value = "公告类型")
    private NoticeType type;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (StringUtils.isNotBlank(this.keyword)) {
            builder.must(QueryBuilders.wildcardQuery("content.keyword", "*" + StringUtils.trim(this.keyword) + "*"));
        }
        if (StringUtils.isNotBlank(this.name)) {
            builder.must(QueryBuilders.wildcardQuery("operatorName.keyword", "*" + StringUtils.trim(this.name) + "*"));
        }
        if (Objects.nonNull(this.type)) {
            builder.must(QueryBuilders.matchPhraseQuery("type.keyword", this.type.name()));
        }
        return builder;
    }
}
